<?php
require '../../config.php';

$idcliente=$_REQUEST['idcliente'];

$query="select con.idcontratos,cli.nombre as cliente,cli.fechacreacion,con.fechainicio,con.fechafin,con.pauta,
con.forma,con.monto,con.saldo,con.letras,con.fechaprox,prop.medio,prop.tipopropuesta,
prop.descripcion,con.observacion,usu.nombre,usu.apellido,cli.activo
from contratos con,clientesventas cli, propuestas prop, usuarios usu
where con.cliente = cli.idclientesventas
and con.propuesta = prop.idpropuestas

and con.vendedor = usu.idusuarios
and con.cliente = ".$idcliente." limit 1";
//echo $query;
//exit();
$query2="SELECT idcontratos FROM hossbd.contratos where cliente = ".$idcliente." and activo = 1";
$query3="SELECT idcontratos FROM hossbd.contratos where cliente = ".$idcliente." and activo = 0";
$subquery="SELECT fechapago,monto,observacion FROM hossbd.pagosclientesventas where idcontrato = ";

$resultset = mysql_query($query,$conexion);

if(!$resultset)
{
    mysql_close();
    echo ('error en el query: ' . mysql_error());
}
elseif(!mysql_num_rows($resultset))
{
    mysql_close();
    echo "<legend>'Sin resultados'</legend>";
}
else
{
	$row = mysql_fetch_assoc($resultset);
	echo   "<table class='table table-bordered'>";
	echo   "<tbody>";
	echo   "<tr>";
				echo   "<td>";
				echo "<form id='formulario' name='formulario'  style='background-color: #f2f9f2; text-align:left'>";
				echo "<div class='form_description'>";
				echo "<h2>Datos Generales del Cliente y Contrato</h2>";	
				echo "<ul>";
				echo "<li id='li_4' >";
				
				echo "<div>";
				echo "<label class='description' for='cedulaver'>Cliente: </label>";
				echo "<input id='cedulaver' disabled type='text'  size='50' value='".$row['cliente']."'>";
				echo "</div>";
				
				echo "<div>";
				echo "<label class='description' for='nombrever'>Fecha de Creaci&oacute;n: </label>";
				echo "<input id='nombrever'  disabled   type='text'  size='50' value='".date('d-m-Y',strtotime($row['fechacreacion']))."'>";
				echo "</div>";
				echo "<legend>Detalle del Contrato</legend>";
				echo "<div id='divcontratos'>";
				echo "<div>";
				echo "<label class='description' for='telefonover'>N&uacute;mero de Contrato: </label>";
				echo "<select id='conts' onchange='cambiarContrato();'>";
				$result2 = mysql_query($query2,$conexion);
				if(mysql_num_rows($result2)>0)
				{
					while($row2 = mysql_fetch_assoc($result2))
					{
						if ($row2['idcontratos'] == $row['idcontratos'])
						 	echo "<option value=".$row2['idcontratos']." selected>".$row2['idcontratos']."</option>";
						else 
							echo "<option value=".$row2['idcontratos'].">".$row2['idcontratos']."</option>";
					}
					
				}
				echo "</select>";
				//echo "<input id='telefonover'  disabled  type='text'  size='50' value='".$row['idcontratos']."'>";
				
				echo "</div>";
				
				echo "<div>";
				echo "<label class='description' for='telefonover2'>Fecha de Inicio: </label>";
				echo "<input id='telefonover2'  disabled  type='text' size='50' value='".date('d-m-Y',strtotime($row['fechainicio']))."'>";
				echo "</div>";
				echo "<div>";
				echo "<label class='description' for='telefonover22'>Fecha de Finalizaci&oacute;n: </label>";
				echo "<input id='telefonover22'  disabled  type='text' size='50' value='".date('d-m-Y',strtotime($row['fechafin']))."'>";
				echo "</div>";
				echo "<div>";
				echo "<label class='description' for='telefonover22w'>Propuesta Aceptada: </label>";
				echo "<input id='telefonover22w'  disabled  type='text' size='50' value='".$row['medio']."'>";
				echo "</div>";
				echo "<div>";
				echo "<input id='telefonover22w1'  disabled  type='text' size='50' value='".$row['tipopropuesta']."'>";
				echo "</div>";
				echo "<div>";
				echo "<input id='telefonover22w2'  disabled  type='text' size='50' value='".$row['descripcion']."'>";
				echo "</div>";
				
				echo "<div>";
				echo "<label class='description' for='emailver'>Tiempo de Pauta: </label>";
				if($row['pauta']==1)
					$pauta="Semanal";
				if($row['pauta']==2)
					$pauta="Quincenal";
				if($row['pauta']==3)
					$pauta="Mensual";
				if($row['pauta']==4)
					$pauta="Trimestral";
				if($row['pauta']==5)
					$pauta="Anual";
				echo "<input id='emailver' disabled type='text'  size='50' value='".$pauta."'>";
				
				echo "</div>";
				
				echo "<div>";
				echo "<label class='description' for='cumplever'>Forma de Pago: </label>";
				if($row['forma']==1)
					$pauta="Efectivo";
				if($row['forma']==2)
					$pauta="Intercambio";
				echo "<input id='cumplever' disabled type='text'  size='50' value='".$pauta."'>";		
				echo "</div>";
				
				echo 	"<div>";
				echo 	"<label class='description' >Vendedor: </label>";
				echo 	"<input disabled   type='text'  size='50' value='".$row['nombre']." ".$row['apellido']."'>";
				echo 	"</div>";
				echo 	"<div>";
				echo 	"<label class='description' >Observaci&oacute;n del Contrato: </label>";
				echo 	"<textarea disabled rows='10' cols='75'>".$row['observacion']."</textarea>";
				echo 	"</div>";
				echo    "<legend>Detalle Financiero</legend>";
				echo 	"<div>";
				echo 	"<label class='description' for='profesionver'>Monto del Contrato: </label>";
				echo 	"<input id='profesionver'  disabled   type='text'  size='50' value='".$row['monto']."'>";
				echo 	"</div>";
				echo 	"<div>";
				echo 	"<label class='description' for='profesionver'>Saldo Actual: </label>";
				echo 	"<input id='profesionver'  disabled   type='text'  size='50' value='".$row['saldo']."'>";
				echo 	"</div>";
				
				$numcontrato = $row['idcontratos'];
				$subquery = $subquery.$numcontrato." order by idpagosclientesventas desc limit 10";
				$subquery1="SELECT count(*) as cantidad FROM hossbd.pagosclientesventas where idcontrato = ".$numcontrato;
				$subquery2="SELECT fechapago FROM hossbd.pagosclientesventas
							where idcontrato = ".$numcontrato."
							order by idpagosclientesventas desc limit 1";
				$result2 = mysql_query($subquery2,$conexion);
				if(mysql_num_rows($result2)>0)
				{
					$row2 = mysql_fetch_assoc($result2);
					$ultimopago = $row2['fechapago'];
				}
				else 
					$ultimopago = "Sin Registro";
				$result2 = mysql_query($subquery1,$conexion);
				if(mysql_num_rows($result2)>0)
				{
					$row2 = mysql_fetch_assoc($result2);
					$cantidadpagos = $row2['cantidad'];
				}
				else
					$cantidadpagos = "0";
				
				
				echo 	"<div>";
				echo 	"<label class='description' for='profesionverw'>Pagos Realizados: </label>";
				echo 	"<input id='profesionverw'  disabled   type='text'  size='50' value='".$cantidadpagos." de ".$row['letras']."'>";
				echo 	"</div>";
				echo 	"<div>";
				echo 	"<label class='description' for='profesionverw'>Fecha de &Uacute;ltimo Pago: </label>";
				echo 	"<input id='profesionverw'  disabled   type='text'  size='50' value='".date('d-m-Y',strtotime($ultimopago))."'>";
				echo 	"</div>";
				echo 	"<div>";
				echo 	"<label class='description' for='profesionverw'>Fecha de Pr&oacute;ximo Pago: </label>";
				echo 	"<input id='profesionverw'  disabled   type='text'  size='50' value='".date('d-m-Y',strtotime($row['fechaprox']))."'>";
				echo 	"</div>";
				echo 	"<div>";
				echo "<div>";
				$result2 = mysql_query("select p.actualizar,p.insertar from permisos p,modulos m where p.idmodulo = m.idmodulos and p.idrol= '". $_SESSION['rol'] ."' and m.nombre = 'Ventas'",$conexion);
				if(mysql_num_rows($result2)>0)
				{
					$row2 = mysql_fetch_assoc($result2);
					if($row2['actualizar']== 1 and $row['activo']== 1)
					{
						echo 	"<button type='button' class='btn btn-link btn-xs'  onclick='desactivar(".$numcontrato.")'><span class='glyphicon glyphicon-pencil'></span> Desactivar Contrato</button>";	
					} 
					if($row2['insertar']== 1 and $row['activo']== 1 )
					{
						echo   	"<button type='button' class='btn btn-link btn-xs' title='Registrar Pago del Cliente' onclick='regpago(".$idcliente.")'><span class='glyphicon glyphicon-pencil'></span> Registrar Pago</button>";
						echo 	"<button type='button' class='btn btn-link btn-xs' title='Agregar Nuevo Contrato'  onclick='nuevoContrato(".$idcliente.")'><span class='glyphicon glyphicon-pencil'></span> Nuevo Contrato</button>";
						$query4="select idcontrato from ordenestrabajo where idcontrato=".$numcontrato." limit 1";
						$result3=mysql_query($query4);
						if(mysql_num_rows($result3)==0)
							echo   "<button type = button class='btn btn-link btn-xs' data-toggle='tooltip' data-placement='top' title='Enviar OT a Producci&oacute;n'  onclick='enviarOT(".$idcliente.",".$numcontrato.")'><span class='glyphicon glyphicon-pencil'></span> Enviar OT</button>";
						
					}
				}
				
				
				
				
				
				echo 	"</div>";
				echo   "</li>";
				echo   "</ul>";
				echo   "</form>";
				echo   "</td>";
				echo   "<td >";
						$resultset2 = mysql_query($subquery,$conexion);
						echo   "<table class='table table-bordered' style='background-color: #f2f9f2'>";
						echo   "<caption><strong>Pagos Realizados (&uacute;ltimos 10)</strong></caption>";
						echo   "<tr class='success'>";
						echo   "<th>Fecha</th><th>Monto</th>";
						echo   "</tr>";
						while($row2 = mysql_fetch_assoc($resultset2))
						{
							echo   "<tr>";
							echo   "<td>".date('d-m-Y',strtotime($row2['fechapago']))."</td>";
							echo   "<td>$".$row2['monto']."</td>";
							echo   "</tr>";
						}
							echo   "<tr class='warning'>";
							echo   "<td colspan='2'><button type='button' class='btn btn-link btn-xs' onclick='verHistorialPagos(".$numcontrato.");' ><span class='glyphicon glyphicon-eye-open'></span> Ver detalles</button> </td>";
							echo   "</tr>";
							echo   "<tr class='info'>";
							if($row['activo']== 1)
								echo    "<td colspan='2'><button type='button' class='btn btn-link btn-xs'  onclick='activos();'><span class='glyphicon glyphicon-arrow-left'></span><font size='3'> Regresar</font></button></td>";
							else
								echo    "<td colspan='2'><button type='button' class='btn btn-link btn-xs'  onclick='reportar(1);'><span class='glyphicon glyphicon-arrow-left'></span><font size='3'> Regresar</font></button></td>";
							
							echo   "</tr>";
						echo   "<tbody>";
						echo   "</tbody>";
						echo   "</table>";
						$result2 = mysql_query($query3,$conexion);
						if(mysql_num_rows($result2)>0)
						{							
							echo   "<table class='table table-bordered' style='background-color: #f2f9f2'>";
								echo   "<caption><strong>Lista de Contratos Desactivados</strong></caption>";
								echo   "<tr class='success'>";
								echo   "<th>Contrato</th><th>Opci&oacute;n</th>";
								echo   "</tr>";
								
									echo   "<tr>";
									echo "<td><select id='conts2' >";
									
										
										
											while($row2 = mysql_fetch_assoc($result2))
												{
													
														echo "<option value=".$row2['idcontratos']." selected>".$row2['idcontratos']."</option>";
													
												}
								
										
									echo "</select></td>";
									
									echo "<td><button type='button' class='btn btn-link btn-xs'  onclick='activar()'><span class='glyphicon glyphicon-pencil'></span> Activar Contrato</button></td>";
									echo   "</tr>";
									echo   "<tbody>";
								echo   "</tbody>";
								echo   "</table>"; 
						}
						
						
						
							
				echo   "</td>";
			echo   "</tr>";
			echo   "</tbody>";
			echo   "</table>";
			
	mysql_close();
		
}


?>